#include "fv.h"
#include "termbox2.h"
#include "inner.h"
// #include "inner.h"

static void handle_mouse() {
}

struct fv_init *fv_init(void) {
    memset(&fvglob, 0, sizeof(fvglob));

    tb_init();
    tb_set_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE);
    tb_set_output_mode(TB_OUTPUT_TRUECOLOR);
    // tb_clear();
    // tb_present();
}

inline void fv_exit(void) {
    tb_shutdown();
}

inline void fv_clear(void) {
    tb_clear();
}

inline void fv_show(void) {
    tb_present();
}

static int fv_loop(void) {
    int ret;
    struct tb_event ev;

    while (1) {
        ret = tb_poll_event(&ev);

        if (ret != TB_OK) {
            if (ret == TB_ERR_POLL && tb_last_errno() == EINTR) {
                /* poll was interrupted, maybe by a SIGWINCH; try again */
                continue;
            }
            /* some other error occurred; bail */
            break;
        }

        switch (ev.type) {
            case TB_EVENT_KEY:
                if (ev.key == TB_KEY_CTRL_C || ev.ch == 'q') {
                    tb_shutdown();
                }
                // printf("\r key=%x\n", ev.key); // XXX: everykey is 0
                break;
            case TB_EVENT_MOUSE:
                break;
            case TB_EVENT_RESIZE:
                break;
            default:
                break;
        }
    }

    return 0;
}

void fv_draw() {
}

